package com.godyao.framework.beans.factory;

import com.godyao.framework.beans.PropertyValue;
import com.godyao.framework.beans.PropertyValues;
import com.godyao.framework.beans.factory.config.BeanDefinition;
import com.godyao.framework.beans.factory.config.BeanFactoryPostProcessor;
import com.godyao.framework.core.io.DefaultResourceLoader;
import com.godyao.framework.core.io.Resource;
import com.godyao.framework.utils.StringValueResolver;

import java.io.IOException;
import java.util.Properties;

/**
 * 占位符填充配置类
 * @author godyao
 */
public class PropertyPlaceholderConfigurer implements BeanFactoryPostProcessor {
    private static final String DEFAULT_PLACEHOLDER_PREFIX = "${";

    private static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";

    public void setLocation(String location) {
        this.location = location;
    }

    private String location;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        // 加载属性文件
        DefaultResourceLoader defaultResourceLoader = new DefaultResourceLoader();
        Resource resource = defaultResourceLoader.getResource(location);
        Properties properties = new Properties();
        try {
            properties.load(resource.getInputStream());
            String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
            for (String beanDefinitionName : beanDefinitionNames) {
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName);
                PropertyValues propertyValues = beanDefinition.getPropertyValues();
                for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
                    Object value = propertyValue.getValue();
                    if (!(value instanceof String)) return;
                    String strValue = (String) value;
                    StringBuffer stringBuffer = new StringBuffer(strValue);
                    int startIdx = stringBuffer.indexOf(DEFAULT_PLACEHOLDER_PREFIX);
                    int endIdx = stringBuffer.indexOf(DEFAULT_PLACEHOLDER_SUFFIX);
                    if (startIdx != -1 && endIdx != -1 && startIdx < endIdx) {
                        String propertyKey = stringBuffer.substring(startIdx + 2, endIdx);
                        String propValue = properties.getProperty(propertyKey);
                        propertyValues.addPropertyValue(new PropertyValue(propertyValue.getName(), propValue));

                    }
                }
            }

            // 向容器中添加字符串解析器，供解析@Value注解使用
            StringValueResolver stringValueResolver =new PlaceholderResolvingStringValueResolver(properties);
            beanFactory.addEmbeddedValueResolver(stringValueResolver);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private class PlaceholderResolvingStringValueResolver implements StringValueResolver {
        private final Properties properties;

        public PlaceholderResolvingStringValueResolver(Properties properties) {
            this.properties = properties;
        }

        @Override
        public String resolveStringValue(String strVal) {
            return PropertyPlaceholderConfigurer.this.resolverPlaceHolder(strVal, properties);
        }
    }

    private String resolverPlaceHolder(String strVal, Properties properties) {
        StringBuilder buffer = new StringBuilder(strVal);
        int startIdx = strVal.indexOf(DEFAULT_PLACEHOLDER_PREFIX);
        int stopIdx = strVal.indexOf(DEFAULT_PLACEHOLDER_SUFFIX);
        if (startIdx != -1 && stopIdx != -1 && startIdx < stopIdx) {
            String propKey = strVal.substring(startIdx + 2, stopIdx);
            String propVal = properties.getProperty(propKey);
            buffer.replace(startIdx, stopIdx + 1, propVal);
        }
        return buffer.toString();
    }
}
